//
// Created by xujingyi on 2021/5/15.
//

#include "find/CNN.h"
#include "log.h"
#include <iostream>
//#include<opencv2/ml/ml.hpp>
//#include<opencv2/objdetect/objdetect.hpp>

std::map<int, int> rm::index2name = {//神经网络序号到名称的map
        {0,0},
        {1,1},{2,2},{3,3},{4,4},{5,5},{6,8},{7,9}
};

rm::CNN::CNN(){
    LOGS("开始导入Lenet模型...");
    LOGM("模型路径:%s",LENET_NET_PATH);
        net = cv::dnn::readNetFromTensorflow(LENET_NET_PATH);
    LOGS( "成功导入Lenet模型...");
    LOGS( "%d",rm::index2name[6]);
};

int rm::CNN::forward(const cv::Mat &input,bool is_highland_blind_view){
    cv::Mat blob = cv::dnn::blobFromImage(input, 1.0/255, cv::Size(28,28));
//	image.convertTo(image, CV_32FC3);
//	cv::Mat blob = cv::dnn::blobFromImage(image, 1.0/255, cv::Size(48,36));

    net.setInput(blob);
    cv::Mat output = net.forward();
    //cout << "output size: " << output.size << endl;
    int max_i=0;
    float max_conf=0;
    for(int i=0;i<output.cols;++i){
        if(output.at<float>(0,i)>max_conf) {
            max_i=i;
            max_conf=output.at<float>(0,i);
        }
    }
    if(is_highland_blind_view){
        if((max_i!=1)&&max_conf<_blind_not_one_conf) return index2name[0];
        else if(max_i==1&&max_conf<_blind_one_conf) return index2name[0];
    }
    else{
        if((max_i!=1)&&max_conf<_not_one_conf) return index2name[0];
        else if(max_i==1&&max_conf<_one_conf) return index2name[0];
    }
//    std::cerr<<"Lenet_result:"<<index2name[max_i]<<" "<<"conf:"<<max_conf<<std::endl;
    return index2name[max_i];
};

